package com.itheima.health.controller.admin;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.result.PageResult;
import com.itheima.health.result.QueryPageBean;
import com.itheima.health.result.Result;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.awt.geom.FlatteningPathIterator;
import java.util.List;

@RestController
@RequestMapping("/checkgroup")
@Slf4j
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    @PostMapping("/add")

    public Result add(@RequestBody CheckGroup checkGroup ,Integer[] checkitemIds){

        try {
            checkGroupService.add(checkGroup,checkitemIds);
        } catch (Exception e) {
            return new Result(false,MessageConst.ADD_CHECKGROUP_FAIL);
        }

        return new Result(true,MessageConst.ADD_CHECKGROUP_SUCCESS);
    }

    @GetMapping("/findPage")
    public Result page(QueryPageBean queryPageBean){
        log.info("[检查项-分页查询]data:{}", queryPageBean);
        PageResult pageResult=checkGroupService.pageQuery(queryPageBean);

        return new Result(true, MessageConst.ACTION_SUCCESS,pageResult);
    }

    @PostMapping("/deleteCheckGroupitemById")
    public Result deleteById(@RequestParam Integer id ){
        try {
            checkGroupService.deleteById(id);
        } catch (Exception e) {
            return new Result(false,MessageConst.DELETE_CHECKGROUP_FAIL);
        }

        return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);

    }

    @GetMapping("/findById")
    public Result findById(Integer  id){
      CheckGroup checkGroup=checkGroupService.findById(id);
      if (checkGroup !=null){
          Result result = new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS);
          result.setData(checkGroup);
          return result;
      }
      return new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL);
    }

    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try {
            List<Integer> checkitemIds=checkGroupService.findCheckItemIdsByCheckGroupId(id);
            return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkitemIds);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_CHECKITEM_FAIL);

        }


    }
    @PostMapping("/edit")
    public Result edit(Integer[] checkitemIds,@RequestBody CheckGroup checkGroup) {
        try {

            checkGroupService.edit(checkGroup,checkitemIds);
            return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
            

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_CHECKGROUP_FAIL);
        }

    }
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("检查组-查询所有");
        List<CheckGroup> checkGroups=checkGroupService.findAll();

        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroups);
    }

}
